package com.mingqi.enetconnected.util;

import android.app.Activity;

import com.mingqi.enetconnected.view.Dialog.LoadingDialog;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;


/**
 * @author li
 * @version 1.0
 * @describe
 * @WeChat 18571658038
 * @date 2018/9/19 星期三.
 */

public class AsyncUtils {

    private LoadingDialog mLoadingDialog;
    private Disposable mDisposable;
    private Activity mActivity;

    public AsyncUtils(Activity activity) {
        mActivity = activity;

        initDialog();
    }

    private void initDialog() {

        mLoadingDialog =new LoadingDialog.Builder(mActivity)
                .setMessage("加载中...")
                .setCancelable(true)
                .setCancelOutSide(true)
                .create();
    }

    private void showDialog(){

        if(mLoadingDialog != null) {

            mLoadingDialog.show();
        } else {

            initDialog();
            mLoadingDialog.show();
        }
    }

    private void hideDialog(){

        if(mLoadingDialog != null && mLoadingDialog.isShowing()) {

            mLoadingDialog.cancel();
        }
    }

    public <T> void execute(final IAsyncListener<T> listener){

        Observable.create(new ObservableOnSubscribe<T>() {
            @Override
            public void subscribe(ObservableEmitter<T> emitter) throws Exception {

                emitter.onNext(listener.getData());
            }
        }).subscribeOn(Schedulers.io())
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {

                        mDisposable = disposable;
                        showDialog();
                    }
                }).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<T>() {
                    @Override
                    public void accept(T s) throws Exception {

                        if((mActivity != null && !mActivity.isFinishing())) {

                            listener.handleData(s);
                            hideDialog();
                        } else {

                            if(mDisposable != null && !mDisposable.isDisposed()) {

                                mDisposable.dispose();
                            }
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                        listener.handleError(throwable);
                        hideDialog();
                    }
                });
    }

    public interface IAsyncListener<T>{

        T getData();
        void handleData(T t);
        void handleError(Throwable throwable);
    }
}
